package com.woniu.order.service.consumer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.order.infra.model.Order;
import com.woniu.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
@RocketMQMessageListener(topic = "lxlg-order-payok",consumerGroup = "lxlg-order-payok")
public class PayOrderSuccessConsuer implements RocketMQListener<String>{
	
	@Resource
	private OrderService orderService;
	
	@Resource
	private StringRedisTemplate srt;
	
	@Override
	public void onMessage(String message) {
		String state = srt.opsForValue().get("pay:success:state:"+message);
		if(state == null || !state.equals("1")) {
			log.info("执行支付成功逻辑");
			Order order = orderService.getOne(new QueryWrapper<Order>().eq("order_num", message));
			orderService.setPayOk(order.getOrderId());
			srt.opsForValue().setIfAbsent("pay:success:state:"+message, "1", 10, TimeUnit.MINUTES);
		}
	}

} 
